package com.ruoyi.cngj.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.cngj.domain.CngjPost;
import com.ruoyi.cngj.service.ICngjPostService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 招聘岗位Controller
 * 
 * @author yuexl
 * @date 2024-06-27
 */
@Controller
@RequestMapping("/cngj/post")
public class CngjPostController extends BaseController
{
    private String prefix = "cngj/post";

    @Autowired
    private ICngjPostService cngjPostService;

    //@RequiresPermissions("cngj:post:view")
    @GetMapping()
    public String post()
    {
        return prefix + "/post";
    }

    /**
     * 查询招聘岗位列表
     */
    //@RequiresPermissions("cngj:post:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CngjPost cngjPost)
    {
        startPage();
        List<CngjPost> list = cngjPostService.selectCngjPostList(cngjPost);
        return getDataTable(list);
    }

    /**
     * 导出招聘岗位列表
     */
    //@RequiresPermissions("cngj:post:export")
    @Log(title = "招聘岗位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CngjPost cngjPost)
    {
        List<CngjPost> list = cngjPostService.selectCngjPostList(cngjPost);
        ExcelUtil<CngjPost> util = new ExcelUtil<CngjPost>(CngjPost.class);
        return util.exportExcel(list, "招聘岗位数据");
    }

    /**
     * 新增招聘岗位
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存招聘岗位
     */
    //@RequiresPermissions("cngj:post:add")
    @Log(title = "招聘岗位", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CngjPost cngjPost)
    {
        return toAjax(cngjPostService.insertCngjPost(cngjPost));
    }

    /**
     * 修改招聘岗位
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        CngjPost cngjPost = cngjPostService.selectCngjPostById(id);
        mmap.put("cngjPost", cngjPost);
        return prefix + "/edit";
    }

    /**
     * 修改保存招聘岗位
     */
    //@RequiresPermissions("cngj:post:edit")
    @Log(title = "招聘岗位", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CngjPost cngjPost)
    {
        return toAjax(cngjPostService.updateCngjPost(cngjPost));
    }

    /**
     * 删除招聘岗位
     */
    //@RequiresPermissions("cngj:post:remove")
    @Log(title = "招聘岗位", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(cngjPostService.deleteCngjPostByIds(ids));
    }

    /**
     * 改变位置
     */
    @Transactional
    @PostMapping("/postMove")
    @ResponseBody
    public AjaxResult postMove(String type,String id,String changeId)
    {
        CngjPost cngjPost = cngjPostService.selectCngjPostById(id);
        CngjPost changeCngjPost = cngjPostService.selectCngjPostById(changeId);
        int num =0;
        if("1".equals(type) || "2".equals(type)){
            //上移
            int currSort = cngjPost.getPostSort();
            cngjPost.setPostSort(changeCngjPost.getPostSort());
            changeCngjPost.setPostSort(currSort);
            num +=cngjPostService.updateCngjPost(cngjPost);
            num +=cngjPostService.updateCngjPost(changeCngjPost);
        }
        if("3".equals(type)){
            cngjPost.setPostSort(Integer.parseInt(changeId)+1);
            num +=cngjPostService.updateCngjPost(cngjPost);
        }
        return toAjax(num);
    }

    /**
     * 启用停用招聘管理素材
     */
    @Log(title = "启用停用招聘管理素材")
    @PostMapping( "/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(String id,String postState)
    {
        CngjPost cngjPost = new CngjPost();
        cngjPost.setId(id);
        cngjPost.setPostState(postState);
        return toAjax(cngjPostService.updateCngjPost(cngjPost));
    }

}
